package it.iubar.tkdscore;

import it.iubar.swing.Global;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

/**
 *
 * @author borgo
 */
public class ControllersWorkerClass {

   static private ControllersThread thread = new ControllersThread();
    private static boolean running = false;

    /**
     * When the worker needs to update the GUI we do so by queuing
     * a Runnable for the event dispatching thread with
     * SwingUtilities.invokeLater().  In this case we're just
     * changing the progress bars value.
     */
    static void updateStatus(final int i) {
        Runnable doSetProgressBarValue = new Runnable() {

            public void run() {
                //progressBar.setValue(i);
               System.out.println("i: " + i);
            }
        };
        SwingUtilities.invokeLater(doSetProgressBarValue);
    }

    /**
     * This method represents the application code that we'd like to
     * run on a separate thread.  It simulates slowly computing
     * a value, in this case just a string 'All Done'.  It updates the
     * progress bar every half second to remind the user that
     * we're still busy.
     */
    static Object doWork() {

        thread.run();

//        try {
//           // for (int i = 0; i < NUMLOOPS; i++) {
//            int i = 0;
//            while(running){
//                updateStatus(i);
//                if (Thread.interrupted()) {
//                    throw new InterruptedException();
//                }
//                Thread.sleep(500);
//                i++;
//            }
//
//        } catch (InterruptedException e) {
//            updateStatus(0);
//            return "Interrupted";  // SwingWorker.get() returns this
//        }

        return new String("Done");

    }

    /* Invoking start() on a SwingWorker causes a new Thread
     * to be created that will run the worker.construct()
     * method we've defined here.
     */
    
    static final SwingWorker worker = new SwingWorker() {

        @Override
        protected Object doInBackground() throws Exception {
        /** Do work that would freeze GUI here */
         return doWork();
        }

        public void finished() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        protected void done() {
        try {
          Object result = get();
          /** Update GUI here */
          System.out.println("Done: " + result);
        }catch (InterruptedException ex) {
          ex.printStackTrace();
        }catch (ExecutionException ex) {
          ex.printStackTrace();
        }catch ( Exception ex ) {
          ex.printStackTrace();
        }
        }
        
    };

 
    /**
     * This action listener, called by the "Cancel" button, interrupts
     * the worker thread which is running this.doWork().  Note that
     * the doWork() method handles InterruptedExceptions cleanly.
     */
    ActionListener interruptListener = new ActionListener() {

        public void actionPerformed(ActionEvent event) {
        stop();
        }
    };
    /**
     * This action listener, called by the "Start" button, effectively
     * forks the thread that does the work.
     */
    ActionListener startListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
        start();
        }
    };


    public static void start(){
            ControllersThread.init();
            running = true;
            worker.execute();
    }

    public static void stop(){
            running = false;
            worker.cancel(true);
    }

} // end class
